//
//  CourseListHalfCell.m
//  xpkc
//
//  Created by TopSage on 15/10/29.
//  Copyright (c) 2015年 TopSage. All rights reserved.
//

#import "CourseListHalfCell.h"

#import "FMDBManager.h"

#import "VideoListModel.h"

@implementation CourseListHalfCell{
    UIImageView *favoriteFlag;
    UIImageView *newFlag;
    VideoListModel * _videoListModel;
    
}

- (id)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
        [self creatCellView];
    }
    return self;
}
/**
 *  创建视图
 */
-(void)creatCellView{
    //总高为40
    UIView *pointView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 40)];
    if (isPad) {
        self.line = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 1, 60)];
    }else{
        self.line = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, 1, 40)];
    }
    self.line.layer.borderWidth = 1;
    self.line.layer.borderColor = GreenColor.CGColor;
    [pointView addSubview:self.line];
    if (isPad) {
        UILabel *point = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 20, 11)];
        point.layer.cornerRadius = 10;
        //    point.backgroundColor = [UIColor blueColor];
        point.text = @"●";
        point.font = [UIFont systemFontOfSize:24];
        point.textAlignment = NSTextAlignmentCenter;
        point.textColor = GreenColor;
        [pointView addSubview:point];
    }else{
        UILabel *point = [[UILabel alloc]initWithFrame:CGRectMake(10, 15, 20, 11)];
        point.layer.cornerRadius = 10;
        //    point.backgroundColor = [UIColor blueColor];
        point.text = @"●";
        point.font = [UIFont systemFontOfSize:24];
        point.textAlignment = NSTextAlignmentCenter;
        point.textColor = GreenColor;
        [pointView addSubview:point];
    }
    //    pointView.backgroundColor = [UIColor redColor];
    
    self.lectureChapter = [[UILabel alloc]init];
    self.teacher = [[UILabel alloc]init];
    self.duration = [[UILabel alloc]init];
    self.downState = [[UILabel alloc]init];
    
    UIImageView *teacherImg = [[UIImageView alloc]init];
    UIImageView *durationImg = [[UIImageView alloc]init];
    
    CGRect chapter_frame;
    CGRect teacher_frame,teacher_img_frame;
    CGRect duration_frame,duration_img_frame;
    CGRect downstate_frame,state_frame;
    CGRect favorite_frame,new_frame;
    if (isPad) {
        chapter_frame = CGRectMake(40, 10, SCREEN_WIDTH-40-60, 30);
        teacher_img_frame = CGRectMake(40, 45, 20, 20);
        teacher_frame = CGRectMake(75, 43, 50, 25);
        duration_img_frame = CGRectMake(160, 43, 20, 20);
        duration_frame = CGRectMake(235, 43, 40, 25);
        favorite_frame = CGRectMake(270, 37, 17, 17);
        new_frame = CGRectMake(300, 37, 17, 17);
        downstate_frame = CGRectMake(SCREEN_WIDTH-40-10-40, 43, 40, 25);
        state_frame = CGRectMake(SCREEN_WIDTH-38, 20, 40, 40);
    }else{
        chapter_frame = CGRectMake(40, 0, SCREEN_WIDTH-40-60, 20);
        teacher_img_frame = CGRectMake(40, 20, 20, 20);
        teacher_frame = CGRectMake(65, 20, 50, 20);
        duration_img_frame = CGRectMake(110, 20, 17, 17);
        duration_frame = CGRectMake(135, 20, 40, 20);
        favorite_frame = CGRectMake(170, 20, 17, 17);
        new_frame = CGRectMake(SCREEN_WIDTH-38, 25, 25, 10);
        downstate_frame = CGRectMake(SCREEN_WIDTH-40-10-30, 20, 40, 20);
        state_frame = CGRectMake(SCREEN_WIDTH-38, 10, 40, 40);
    }
    
    self.lectureChapter.frame = chapter_frame;
    self.teacher.frame = teacher_frame;
    self.duration.frame = duration_frame;
    self.downState.frame = downstate_frame;
    teacherImg.frame = teacher_img_frame;
    durationImg.frame = duration_img_frame;
    
    favoriteFlag = [[UIImageView alloc]initWithFrame:favorite_frame];
    favoriteFlag.image = [UIImage imageNamed:@"heart_gray"];
    [self.contentView addSubview:favoriteFlag];
    
    newFlag = [[UIImageView alloc]initWithFrame:new_frame];
    newFlag.image = [UIImage imageNamed:@"Img_new_flag"];
    [self.contentView addSubview:newFlag];
    
    
    // 收藏
    self.favoriteBtn = [[UIButton alloc] initWithFrame:favorite_frame];
    [self.favoriteBtn setImage:[UIImage imageNamed:@"heart_gray"] forState:UIControlStateNormal];
    //    NSDictionary *dict = [self.myFavoriteArray objectAtIndex:self.indexPath.section];
    //    NSArray *array = [dict objectForKey:@"array"];
    //    VideoListModel *model = [array objectAtIndex:self.indexPath.row];
    
    
    
    [self.favoriteBtn addTarget:self action:@selector(favoriteBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.contentView addSubview:self.favoriteBtn];
    
    
    //
    self.lectureChapter.font = FONT_SYS_LITTLE;
    self.lectureChapter.numberOfLines = 1;
    self.teacher.font = FONT_SYS_LITTLE;
    self.duration.font = FONT_SYS_LITTLE;
    self.downState.font = FONT(12);
    self.downState.textColor = [UIColor lightGrayColor];
    teacherImg.image =
    [UIImage imageNamed:@"Img_teacher"];
    durationImg.image =
    [UIImage imageNamed:@"Img_duration"];
    self.teacher.textColor = [UIColor darkGrayColor];
    self.duration.textColor = [UIColor darkGrayColor];
    
    self.downState.backgroundColor = [UIColor clearColor];
    
    [self.contentView addSubview:pointView];
    [self.contentView addSubview:self.lectureChapter];
    [self.contentView addSubview:teacherImg];
    [self.contentView addSubview:self.teacher];
    [self.contentView addSubview:durationImg];
    [self.contentView addSubview:self.duration];
    [self.contentView addSubview:self.downState];
    if (isPad) {
        UILabel *Vline = [[UILabel alloc]initWithFrame:CGRectMake(27, 59, SCREEN_WIDTH-20, 1)];
        Vline.layer.borderWidth = 1;
        Vline.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1].CGColor;
        [self.contentView addSubview:Vline];
    }else{
        UILabel *Vline = [[UILabel alloc]initWithFrame:CGRectMake(25, 39, SCREEN_WIDTH-20, 1)];
        Vline.layer.borderWidth = 1;
        Vline.layer.borderColor = [UIColor colorWithRed:0.8 green:0.8 blue:0.8 alpha:1].CGColor;
        [self.contentView addSubview:Vline];
    }
}

-(void)configCellWithVideolistModel:(VideoListModel *)videoListModel{
    
    _videoListModel = videoListModel;
    
    // NSLog(@"%@",videoListModel.videoId);
    if ([[FMDBManager shareFMDBManager] selectVideoCollectWithvideoId:videoListModel.videoId]) {
        
        [self.favoriteBtn setImage:[UIImage imageNamed:@"heart_red"] forState:UIControlStateNormal];
        // self.isFavorite = YES;
    }else{
        [self.favoriteBtn setImage:[UIImage imageNamed:@"heart_gray"] forState:UIControlStateNormal];
    }
    
}

// 添加收藏
-(void)favoriteBtnClick{
    
    // NSLog(@"----收藏");
    
    
    //    NSDictionary *dict = [self.myFavoriteArray objectAtIndex:self.indexPath.section];
    //
    //
    //    NSArray *array = [dict objectForKey:@"array"];
    VideoListModel *model = _videoListModel;//[array objectAtIndex:self.indexPath.row];
    
    NSLog(@"videoId--%@--%@--%@--%@",model.videoId,model.section,model.lecture,model.title);
    
    if ([[FMDBManager shareFMDBManager] insertVideoCollectWithvideoId:model.videoId indexpath:self.indexPath lecture_section:model.section teacher:model.teacher duration:model.duration]) {
        
        
        [self.favoriteBtn setImage:[UIImage imageNamed:@"heart_red"] forState:UIControlStateNormal];
    }else{
        
        
        if ([[FMDBManager shareFMDBManager] deleteVideoCollectDataWithvideoId:model.videoId]) {
            
            [self.favoriteBtn setImage:[UIImage imageNamed:@"heart_gray"] forState:UIControlStateNormal];
        }
        
    }
    
    
}



- (void)setIsFavorite:(BOOL)isFavorite{
    //
    if (isFavorite) {
        favoriteFlag.image = [UIImage imageNamed:@"heart_red"];
    }else{
        favoriteFlag.image = [UIImage imageNamed:@"heart_gray"];
    }
}
- (void)setIsNew:(BOOL)isNew{
    newFlag.hidden = !isNew;
}
- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];
    
    // Configure the view for the selected state
}

@end
